package com.tupu.service.impl.dispatch;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tupu.entity.dispatch.SignalOmissionPointTableMsg;
import com.tupu.entity.ocs.T1216Devicetype;
import com.tupu.mapper.dispatch.SignalOmissionPointTableMsgMapper;
import com.tupu.mapper.ocs.T1216DevicetypeMapper;
import com.tupu.service.dispatch.SignalOmissionPointTableMsgService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;


@Service
public class SignalOmissionPointTableMsgServiceImpl extends ServiceImpl<SignalOmissionPointTableMsgMapper, SignalOmissionPointTableMsg> implements SignalOmissionPointTableMsgService {

    @Autowired(required = false)
    private SignalOmissionPointTableMsgMapper signalOmissionPointTableMsgMapper;

    @Autowired(required = false)
    private T1216DevicetypeMapper t1216DevicetypeMapper;

    @Override
    public List<String> findAllDeviceType(String deviceType) {
        QueryWrapper<T1216Devicetype> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(deviceType)) {
            queryWrapper.like("C1216_DEVTYPEDESC", deviceType);
        }
        List<T1216Devicetype> t1216Devicetypes = t1216DevicetypeMapper.selectList(queryWrapper);
        return t1216Devicetypes.stream().map(T1216Devicetype::getC1216Devtypedesc).collect(Collectors.toList());
    }

    @Override
    public List<String> findAllStation() {
        List<String> list = new ArrayList<>();
        QueryWrapper<SignalOmissionPointTableMsg> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("station");
        List<SignalOmissionPointTableMsg> problemLists = signalOmissionPointTableMsgMapper.selectList(queryWrapper);
        for (SignalOmissionPointTableMsg info : problemLists) {
            if (null != info){
                list.add(info.getStation());
            }
        }
        List<String> stringList = list.stream().distinct().collect(Collectors.toList());
        stringList.remove("");
        return stringList;
    }

    @Override
    public Map<String, Object> stationPercentage() {
        List<String> list = new ArrayList<>();
        QueryWrapper<SignalOmissionPointTableMsg> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("station");
        List<SignalOmissionPointTableMsg> problemLists = signalOmissionPointTableMsgMapper.selectList(queryWrapper);
        for (SignalOmissionPointTableMsg info : problemLists) {
            if (null != info){
                list.add(info.getStation());
            }
        }
        Map<String, Object> result = new HashMap<>();
        double size = list.size() * 1.0;
        Map<String, Long> map = list.stream().collect(Collectors.groupingBy(k -> k, Collectors.counting()));
        Set<String> strings = map.keySet();
        for (String s : strings) {
            DecimalFormat decimalFormat = new DecimalFormat("##.00%");
            String format = decimalFormat.format(map.get(s) / size);
            result.put(s, format);
        }
        return result;
    }
}